package com.zhanglifan.shiro;

import com.zhanglifan.domain.Module;
import com.zhanglifan.domain.Role;
import com.zhanglifan.domain.User;
import com.zhanglifan.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2016/9/26 9:13
 * Description: AuthRealm
 */
public class AuthRealm extends AuthorizingRealm {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        User user = (User) principalCollection.fromRealm(this.getName()).iterator().next();
        System.out.println("*** user 的值是: " + user.getUserName() + " ***");
        List<String> permission = new ArrayList<>();

        Set<Role> roles = user.getRoles();

        for (Role role:roles) {
            Set<Module> modules = role.getModules();
            for (Module module:modules) {
                permission.add(module.getCpermission());
            }
        }
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permission);
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");

        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;

        User user = userService.findUserByUserName(upToken.getUsername());

        System.out.println("*** user 的值是: " + user + " ***");


//         if(user != null){
//            return new SimpleAuthenticationInfo(user,user.getPassword(),getName());
//        }
//        return null;
        return user==null ? null : new SimpleAuthenticationInfo(user,user.getPassword(),getName());
    }


//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
//        // TODO Auto-generated method stub
//        System.out.println("调用了授权方法");
//        User user = (User) pc.getPrimaryPrincipal();
//        Set<Role> roles = user.getRoles();
//
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//
//        for(Role role:roles){
//            Set<Module> modules = role.getModules();
//            for(Module module:modules){
//                info.addStringPermission(module.getCpermission());
//            }
//
//        }
//        return info;
//    }

//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        // TODO Auto-generated method stub
//        System.out.println("调用了认证方法");
//        UsernamePasswordToken userToken =  (UsernamePasswordToken) token;
//
//        //查询数据库中是否存在该用户
//        List<User> userList = userService.find("from User where userName = ?", User.class, new Object[]{userToken.getUsername()});
//
//        if(userList != null && userList.size() > 0){
//            User user = userList.get(0);
//            return new SimpleAuthenticationInfo(user,user.getPassword(),getName());
//        }
//        return null;
//    }
}
